import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import { useBoolean } from 'ahooks'
import zhCN from 'antd/locale/zh_CN'
import { ConfigProvider } from 'antd'
import { lazy, Suspense, useEffect } from 'react'
import { BrowserRouter, useRoutes } from 'react-router-dom'

import { Voice } from './Voice'
import Loading from '../Currency/Loading'
import { VersionTips } from './MenuNav/DiyList/VersionTips'

dayjs.locale('zh-cn')

const { pathname, port } = window.location

// 通用页面
const Login = lazy(() => import('../Login'))
const NO404 = lazy(() => import('../Currency/404'))
const Admin = lazy(() => import('../../Pages/Admin/Admin'))
const Front = lazy(() => import(`../../Pages/Front`))
const Home = lazy(() => import(`../../Pages/Front/Home`))
const Page = lazy(() => import(`../../Pages/Front/Page`))
const Other = lazy(() => import(`../../Pages/Front/Other`))

// 路由
const Router = [
    // 定义页面路径
    { path: '/', element: <Login /> },
    { path: '*', element: <NO404 /> },
    { path: 'Admin', element: <Admin /> },
    { path: 'VersionTips', element: <VersionTips /> },
    {
        path: 'Front',
        element: <Front />,
        children: [
            { path: '', element: <Home /> },
            { path: 'Page/:page', element: <Page /> },
            { path: 'Other/:uuid', element: <Other /> },
        ],
    },
]

const Main = () => {
    const App = () => useRoutes(Router)
    const [EdiTionState, { setFalse }] = useBoolean(true)
    useEffect(() => {
        const MinimumVersion = { Chrome: 93, Safari: 530, Firefox: 92, Opera: 80 }
        const { userAgent } = window.navigator
        const webList = userAgent
            .split(' ')
            .filter(v => v.indexOf('/') !== -1 && v.indexOf('Mozilla') === -1 && v.indexOf('AppleWebKit') === -1)
        const editionData = webList.map(key => ({ [key.split('/')[0]]: parseInt(key.split('/')[1]) }))
        editionData.forEach(key => {
            const [type] = Object.keys(key)
            const [edition] = Object.values(key)
            if (edition <= MinimumVersion[type]) setFalse()
        })
    }, [setFalse])

    useEffect(() => {
        const debuggerFun = () => {
            const block = () => {
                if (window.outerHeight - window.innerHeight > 200 || window.outerWidth - window.innerWidth > 200) {
                    document.body.innerHTML = '检测到非法调试,请关闭后刷新重试!'
                    sessionStorage.clear()
                    localStorage.clear()
                }
                setInterval(() => {
                    ;(function () {
                        return false
                    })
                        [
                            // eslint-disable-next-line no-unexpected-multiline
                            'constructor'
                        ]('debugger')
                        [
                            // eslint-disable-next-line no-unexpected-multiline
                            'call'
                        ]()
                }, 50)
            }
            try {
                block()
            } catch (err) {
                /* empty */
            }
        }
        if (!port) debuggerFun()
    }, [])

    return (
        <ConfigProvider locale={zhCN}>
            <BrowserRouter>
                <Suspense fallback={<Loading />}>{EdiTionState ? <App /> : <VersionTips />}</Suspense>
                {pathname !== '/' && <Voice />}
            </BrowserRouter>
        </ConfigProvider>
    )
}
export default Main
